@echo off
@rem This is where the project-specific tests will go

setlocal

set exe=python_d
set previous=%CD%
set lxml=
set stable=
set pyrex=
set pull=
set LXML_ERROR=0
set tool=%~pd0
set libxml2=%tool%\libxml2-2.6.26.win32
set libxslt=%tool%\libxslt-1.1.17.win32
set zlib=%tool%\zlib-1.2.3.win32
set iconv=%tool%\iconv-1.9.2.win32
set INCLUDE=%libxml2%\include;%libxslt%\include;%zlib%\include;%iconv%\include
set LINK=%libxml2%\lib\libxml2_a.lib %zlib%\lib\zlib.lib %libxslt%\lib\libxslt_a.lib %libxslt%\lib\libexslt_a.lib %iconv%\lib\iconv_a.lib Ws2_32.lib
set LIBRARY=%libxml2%\lib;%zlib%\lib;%libxslt%\lib;%iconv%\lib
PATH %CD%\PCbuild;%PATH%

:CheckOpts
if "%1"=="stable" (set lxml=lxml-1.2.1)	& (set stable=y) & shift & goto CheckOpts
if "%1"=="dev" (set lxml=lxml-trunk) 	& (set pyrex=lxml-pyrex-trunk) & (set pull=y) & shift & goto CheckOpts

if not defined lxml goto Error
if not defined pull goto RunTests
if defined pull goto Pull

:Error
@echo "ERROR: First argument must be one of (stable, dev)"
@set LXML_ERROR=1
@goto End

:Pull
@echo on
@echo "Pulling latest changes from upstream into %tool%%pyrex%"
@if not exist %pyrex% svn co http://codespeak.net/svn/lxml/pyrex %tool%%pyrex%

@cd %tool%%pyrex%
@svn cleanup
@svn up

@set build=%tool%%lxml%-build

@echo "Removing Pyrex install directory"
@%exe% %tool%\rmtree.py %build%

@echo "Installing Pyrex"
@%exe% setup.py -q install --prefix=%build%

@echo "Adding %build%\Lib\site-packages to PYTHONPATH"
@set PYTHONPATH=%build%\Lib\site-packages;%PYTHONPATH%

@echo "Pulling latest changes from upstream into %tool%%lxml%"
@if not exist %lxml% svn co http://codespeak.net/svn/lxml/trunk %tool%%lxml%

@cd %tool%%lxml%
@svn cleanup
@svn up
@echo off

:RunTests
@echo on
@echo "Changing to %tool%%lxml% directory"
@cd %tool%%lxml%

@echo "Doing in-place build"
@echo INCLUDE=%INCLUDE%
@echo LINK=%LINK%
%exe% setup.py build_ext -i --static --debug --force
@if %ERRORLEVEL% NEQ 0 (set LXML_ERROR=%ERRORLEVEL%)

@echo "Adding %CD%\src to PYTHONPATH"
@set PYTHONPATH=%CD%\src;%PYTHONPATH%

@echo "Running basic tests"
%exe% test.py -vv
@if %ERRORLEVEL% NEQ 0 (set LXML_ERROR=%ERRORLEVEL%)

:SelfTests
@echo "Running self tests"
%exe% selftest.py -vv
@if %ERRORLEVEL% NEQ 0 (set LXML_ERROR=%ERRORLEVEL%)

%exe% selftest2.py -vv
@if %ERRORLEVEL% NEQ 0 (set LXML_ERROR=%ERRORLEVEL%)

@echo off
cd %previous%

:End
exit /b %LXML_ERROR%
